

#include "Collector.h"

Collector::Collector(){
	count = 0;
}

Collector::~Collector(){

}

void Collector::inspectSubscription(BackSubPkt *pkt){

	int type = pkt->getEventType();
	boost::unordered_map<int, boost::unordered_map<std::string, std::vector<Type> > >::iterator it = map.find(type);
	if (it!=map.end()) {
		for (int i=0; i<pkt->getConstraintsNum(); i++){
			Constraint constraint = pkt->getConstraint(i);
			if (constraint.op != EQ){
				Type value;
				if (constraint.type == BOOL || constraint.type == STRING) continue;
				if (constraint.type == INT){
					value.type = INT;
					value.intVal = constraint.intVal;
				}
				if (constraint.type == FLOAT){
					value.type = FLOAT;
					value.floatVal = constraint.floatVal;
				}
				boost::unordered_map<std::string, std::vector<Type> >::iterator it2 = it->second.find(constraint.name);
				if (it2 != it->second.end()) {
					it2->second.push_back(value);
				} else {
					std::vector<Type> listType;
					listType.push_back(value);
					it->second.insert(make_pair(constraint.name,listType));
				}
			}
		}
//		cout << "Collezionata sottoscrizione di tipo esistente: " << type << endl;

	} else {
		boost::unordered_map<std::string, std::vector<Type> > attrMap;
		for (int i=0; i<pkt->getConstraintsNum(); i++){
			Constraint constraint = pkt->getConstraint(i);
			if (constraint.op != EQ){
				Type value;
				if (constraint.type == BOOL || constraint.type == STRING) continue;
				if (constraint.type == INT){
					value.type = INT;
					value.intVal = constraint.intVal;
				}
				if (constraint.type == FLOAT){
					value.type = FLOAT;
					value.floatVal = constraint.floatVal;
				}
				std::vector<Type> listType;
				listType.push_back(value);
				attrMap.insert(make_pair(constraint.name,listType));
			}
		}
		if (attrMap.size() != 0){
			map.insert(make_pair(type, attrMap));
//			cout << "Collezionata sottoscrizione di nuovo tipo: " << type << endl;
		} else {
//			cout << "Nulla da collezionare" << endl;
		}


	}
	count++;
}


void Collector::reset(){
	map.clear();
	count = 0;
}
